package Top101K;

/**
 * @Author quan
 * @Description 反转字符串
 * @Date 2023/11/14 10:39
 */
public class BM91_ReverseStr {
    public static void main(String[] args) {
        String str = "abcde";
        String res = solve(str);
        System.out.println(res);
    }
    /**
     * @param str  "abcd"
     * @return     "dcba"
     */

    public static String solve (String str) {
        //1-先写入数组
        char[] arr = new char[str.length()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = str.charAt(i);
        }
        //2-双指针暴力循环
        int i = 0;
        int j = str.length()-1;
        while (true){
            if(i>=j){
                break;
            }
            char temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
        //3-写回字符串
        StringBuilder res = new StringBuilder();
        for (int k = 0; k < arr.length; k++) {
            res.append(arr[k]);
        }
        return res.toString();
    }
}
